package com.icbc.cfsmgmt.timer;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.lang.time.DateUtils;

import com.icbc.cte.base.CTEConstance;

public class TaskManager implements ServletContextListener {
	/**
	* 每天的毫秒数
	*/
	public static final long PERIOD_DAY = DateUtils.MILLIS_PER_DAY;
	
	/**
	* 每分钟的毫秒数
	*/
	public static final long PERIOD_MINUTE = DateUtils.MILLIS_PER_MINUTE;
	/**
	* 一周内的毫秒数
	*/
	public static final long PERIOD_WEEK = PERIOD_DAY * 7;
	/**
	* 延迟10秒
	*/
	public static final long NO_DELAY = PERIOD_MINUTE/6;
	/**
	* 定时器
	*/
	private Timer timer;
	/**
	* 在Web应用启动时初始化任务
	*/
	public void contextInitialized(ServletContextEvent event) {
		//定义定时器
		timer = new Timer("向财富师App发送通知",true); 
//		Calendar calendar = Calendar.getInstance();  
//		calendar.set(Calendar.HOUR_OF_DAY, 23);  
//		calendar.set(Calendar.MINUTE, 0);  
//		calendar.set(Calendar.SECOND, 0);  
		Date time = null;  
		
		Date date1=new Date();
		String day = new SimpleDateFormat("yyyy-MM-dd").format(date1);
		//注册用户同步更新时间
		Date userSynchroTime=null;
		//客户生日提醒时间
		Date custBirthTime=null;
		//在投产品提醒时间
		Date castProductTime=null;
		//缓存文件删除时间
		Date fileInfoDelTime=null;
		
		try {
//			userSynchroTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(day+" "+CTEConstance.getEnvProperty("userSynchroTime"));
//			
//			custBirthTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(day+" "+CTEConstance.getEnvProperty("custBirthTime"));
//			
//			castProductTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(day+" "+CTEConstance.getEnvProperty("castProductTime"));
//			
//			fileInfoDelTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(day+" "+CTEConstance.getEnvProperty("fileInfoDelTime"));
			
			userSynchroTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse((day+1)+" "+"04:00:00");
			
			custBirthTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(day+" "+"23:00:00");
			
			castProductTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(day+" "+"23:00:00");
			
			fileInfoDelTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(day+" "+"23:00:00");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		//启动定时发送通知任务,每半小时执行一次
		//在投产品提醒
		timer.schedule(new BackUpTableTask(),castProductTime, PERIOD_DAY);
		
		//客户生日提醒
		timer.schedule(new BirthdayNotifyTask(),custBirthTime, PERIOD_DAY);
		
		//定时删除缓存数据
		timer.schedule(new FileInfoDelete(),fileInfoDelTime, PERIOD_DAY);
		
		//定时更新用户数据
		timer.schedule(new UpdateCTPUser(),userSynchroTime, PERIOD_DAY);
		

	}
	/**
	* 在Web应用结束时停止任务
	*/
	public void contextDestroyed(ServletContextEvent event) {
		timer.cancel(); // 定时器销毁
	}

	}
